<?php
header("Content-type: text/html; charset=utf-8");
error_reporting(E_ALL || ~E_NOTICE);

	$sqlserver  = '192.168.1.10'; 
    $sqldatabase= 'GT';
    $sqluser    = 'sa2';
    $sqlpassword= 'sasa';
    $sqlconnection = odbc_connect("Driver={SQL Server};Server=$sqlserver;Database=$sqldatabase;", $sqluser, $sqlpassword);
	if(!$sqlconnection){
		echo '数据库连接失败';exit;
	}

	
	if($_POST['data']){
		//修改订单
		$json = $_POST['data'];
		$data = json_decode($json,1);
		//error_log(var_export($data,1));
		$res = updateOrder($data, $sqlconnection);
		echo $res?1:0;
		odbc_close($sqlconnection);
		exit;
	}

	$itemCode  = $_GET['id'];
    $sqlquery="SELECT   a.*,s.WhsName FROM  OITW   as a LEFT JOIN owhs as s on a.WhsCode=s.WhsCode where a.ItemCode='{$itemCode}' ";
	//error_log($sqlquery);

	//数据查询
	$result=odbc_exec($sqlconnection,$sqlquery);  
    while ($data[] = odbc_fetch_array($result));
	odbc_close($sqlconnection);
	$keylist = array_keys($data[0]);
	
	//将结果转码utf-8
	foreach($data as $key=>$value){
		
		foreach($keylist as $keyname){			
			$data[$key][$keyname]=iconv("gb2312","utf-8//IGNORE",$value[$keyname]);
		}
			
	}
	
    echo getHTML($data);
	exit;
	
	
	
	
	
	function getHTML($data){
		$str_head = '<div class="table-a"> <table  style="width:500px;text-align:center"; border="1px" cellspacing="0px" style="border-collapse:collapse"  bordercolor="#000000">
		<tr><td>代码/名称</td><td>可用</td><td>存货量</td><td>已订购</td><td>已承诺</td><td>存捡配</td><tr/>
		';
		$tr = '';
		$allOnHand;$allOnOrder;$allU_TI_Z0_IsCommited;$allU_TI_Z0_IsJp;$allkeyong;
		foreach($data as $vo){
			
			//error_log(var_export($vo,1));
			if(!$vo['WhsCode']){
				continue;
			}
			if($vo['WhsCode']=='A06'){
			    continue;
			}
			
			$allOnHand+=ceil($vo['OnHand']);
			$allOnOrder+=ceil($vo['OnOrder']);
			$allU_TI_Z0_IsCommited+=ceil($vo['U_TI_Z0_IsCommited']);
			$allU_TI_Z0_IsJp+=ceil($vo['U_TI_Z0_IsJp']);
			if(ceil($vo['OnHand'])+ceil($vo['OnOrder'])+ceil($vo['U_TI_Z0_IsCommited'])+ceil($vo['U_TI_Z0_IsJp'])<1){
				continue;
			}
			$vo['keyong'] = ceil($vo['OnHand'])+ceil($vo['OnOrder'])-ceil($vo['U_TI_Z0_IsCommited'])-ceil($vo['U_TI_Z0_IsJp']);
			$allkeyong+=$vo['keyong'];
			
			if($vo['keyong']>0){
			
				$vo['keyong']='<span style="color:green">'.$vo['keyong'].'</span>';
			}else{
				$vo['keyong']='<span style="color:red">'.$vo['keyong'].'</span>';
			}
			
			if(ceil($vo['OnHand'])){
			
				$vo['OnHand']='<span style="color:green">'.ceil($vo['OnHand']).'</span>';
			}else{
				$vo['OnHand']='<span style="color:block">-</span>';
			}
			if(ceil($vo['OnOrder'])){
				$vo['OnOrder']='<span style="color:green">'.ceil($vo['OnOrder']).'</span>';
			}else{
				$vo['OnOrder']='<span style="color:block">-</span>';
			}
			if(ceil($vo['U_TI_Z0_IsCommited'])){
				$vo['U_TI_Z0_IsCommited']='<span style="color:green">'.ceil($vo['U_TI_Z0_IsCommited']).'</span>';
			}else{
				$vo['U_TI_Z0_IsCommited']='<span style="color:block">-</span>';
			}
			if(ceil($vo['U_TI_Z0_IsJp'])){
				$vo['U_TI_Z0_IsJp']='<span style="color:green">'.ceil($vo['U_TI_Z0_IsJp']).'</span>';
			}else{
				$vo['U_TI_Z0_IsJp']='<span style="color:block">-</span>';
			}
			
			$vo['WhsName']=str_replace('仓库', '', $vo['WhsName']);
			$tr.=  '<tr><td style="width:80px;">'.$vo['WhsCode'].'/'.$vo['WhsName'].'</td>'.
				'<td style="width:80px;">'.$vo['keyong'].'</td>'.
				   '<td style="width:80px;">'.$vo['OnHand'].'</td>'.
				   '<td style="width:80px;">'.$vo['OnOrder'].'</td>'.
				   '<td style="width:80px;">'.$vo['U_TI_Z0_IsCommited'].'</td>'.
				   '<td style="width:80px;">'.$vo['U_TI_Z0_IsJp'].'</td>'.
				   '<tr>';        
		}
		$tr.=  '<tr><td style="width:80px;">总计</td>'.
				'<td style="width:80px;">'.$allkeyong.'</td>'.
				   '<td style="width:80px;">'.$allOnHand.'</td>'.
				   '<td style="width:80px;">'.$allOnOrder.'</td>'.
				   '<td style="width:80px;">'.$allU_TI_Z0_IsCommited.'</td>'.
				   '<td style="width:80px;">'.$allU_TI_Z0_IsJp.'</td>'.
				   '<tr>';   
       $end = '</table></div>';
       echo $str_head.$tr.$end;
   
		
		
	}
		
		function updateOrder($data,$conn){
			$filed = ['U_Address','U_Phone','U_Cstname','U_InHead','U_Memo','U_YXJ','U_InType'];
		
			if(!$data['U_OrderId']){
				return false;
			}
			$set = [];
			foreach($filed as $vo){
				$temp = iconv("utf-8","gb2312",$data[$vo]);
				$set[]=$vo.'='."'{$temp}'";
			}
			$strSet = 'set '.implode(',',$set);
			$where = " where U_OrderId='{$data['U_OrderId']}' ";
			$sql = "update [@ti_z0081] " .$strSet . $where."";
			//$sql = "update [@ti_z0081] set U_Address='111'  where U_OrderId='457233345' ";
			//error_log($sql);
			$res = odbc_exec($conn,$sql)or die("<p>".odbc_errormsg());;
			
			return $res ; 
			
			
			
		}
			
		
		
	
